//
//  NetworkTools.swift
//  Weibo
//
//  Created by 潇 on 15/12/28.
//  Copyright © 2015年 潇. All rights reserved.
//

import AFNetworking

//自定义错误的信息
let dataErrorDomain = "com.baidu.data.error"

//Swift 可以时任意类型
enum HTTPMethod: Int {
    case GET = 0
    case POST = 1
}

//网络请求的处理层  以后的网络请求都使用这个类来进行数据的获取
class NetworkTools: AFHTTPSessionManager {

    //单例对象
    static let sharedTools: NetworkTools = {
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL: url)
        //反序列化数据格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    //网络请求的核心方法封装
    func requestJSONDict( method: HTTPMethod, urlString: String, parameters: [String : String]?, finished: (dict: [String : AnyObject]?, error: NSError?) -> ()) {
        if method == HTTPMethod.POST {
        
            //发送 POST 请求
            POST(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
                    //请求成功的回调
//                    print(result)
                    guard let dict = result as? [String : AnyObject] else {
                        //不能够转换为字典数据
                        //执行失败的回调
                        //domain 反转的域名
                        //code 错误的状态码  自定义的一般使用负数
                        //userInfo: NSLocalizedDescriptionKey 错误描述信息
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
                        //错误信息(总)
                        print(myError)
                        finished(dict: nil, error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                    
                }) { (_, error) -> Void in
                    //执行失败的回调
                    finished(dict: nil, error: error)
                    //请求失败的回调
                    print(error)
            }
        }else {
        
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                print(p)
                }, success: { (_, result) -> Void in
                    
//                    print(result)//加载所有的微博数据
                    guard let dict = result as? [String : AnyObject] else {
                        //不能够转换为字典数据
                        //执行失败的回调
                        //domain 反转的域名
                        //code 错误的状态码  自定义的一般使用负数
                        //userInfo: NSLocalizedDescriptionKey 错误描述信息
                        let myError = NSError(domain: dataErrorDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
                        //错误信息(总)
                        print(myError)
                        finished(dict: nil, error: myError)
                        return
                    }
                    //执行成功的回调
                    finished(dict: dict, error: nil)
                }) { (_, error) -> Void in
                    //执行失败的回调
                    finished(dict: nil, error: error)
                    //请求失败的回调
                    print(error)
            }
            
        }
        
    }
    
    
}
